﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class PluginTemplates : AdminPage, IPluginTemplatesView
    {
        private PluginTemplatesPresenter _presenter;


        public override string HelpPage
        {
            get { return "dialogs/plugin-templates.aspx"; }
        }

        #region IPluginTemplatesView Members

        public int PluginID
        {
            get
            {
                if (ViewState["PluginID"] == null)
                    ViewState["PluginID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["PluginID"];
            }
        }


        public void SetPluginTemplates(IList<PluginTemplate> templates)
        {
            rpPluginTemplates.DataSource = templates;
            rpPluginTemplates.DataBind();
            trNoTemplates.Visible = templates.Count == 0;
        }


        public void SetHeading(string pluginName)
        {
            HeadingText = GetText(this, "Heading") + " &mdash; " + pluginName;
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/PluginTemplates.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new PluginTemplatesPresenter(this);
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
                hlBack.NavigateUrl = "Admin/Plugins.aspx";
                hlNewTemplate.NavigateUrl = "Admin/EditPluginTemplate.aspx?PluginID=" + PluginID;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('pluginTemplatePaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }


        protected void rpPluginTemplates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var template = (PluginTemplate) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litDescription = (Literal) e.Item.FindControl("litDescription");
            var litLocalizedTemplate = (Literal) e.Item.FindControl("litLocalizedTemplate");
            var litSystemID = (Literal) e.Item.FindControl("litSystemID");

            litName.Text = template.Name;
            litDescription.Text = template.Description;
            litSystemID.Text = template.ID.ToString();
            if (template.Literals.Count > 0)
            {
                litID.Text = "<span id=\"P" + template.ID + "\" localized=\"true\">";
                litLocalizedTemplate.Text = GetText(this, "llYes");
            }
            else
            {
                litID.Text = "<span id=\"P" + template.ID + "\">";
                litLocalizedTemplate.Text = GetText(this, "llNo");
            }
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteTemplate(int.Parse(hidSelectedPluginTemplateID.Value.Substring(1)));
            upnlPluginTemplates.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initPluginTemplateList",
                                                            "initPluginTemplateList();", true);
        }
    }
}